//
// signal map
//
// SignalMap.h
//
// wangsihong@live.com
//

#ifndef Rooftop_SignalMap_H
#define Rooftop_SignalMap_H

#include <iostream>
#include <iterator>
#include <map>

#include "RFObject.h"

namespace Rooftop {
    template <typename F>
        class SignalMap : public RFObject
        {
            public:

            int insert(std::string key, boost::signals2::signal<F>* sig_ptr)
            {
                m_sigmap.insert(make_pair(key, sig_ptr));
                return m_sigmap.size();
            }

            int remove(std::string key)
            {
                typename std::map<std::string, boost::shared_ptr<boost::signals2::signal<F> > >::iterator it = m_sigmap.find(key);
                if (it == m_sigmap.end()) {
                    Log(LERROR, "can't find \"%s\"\n", key.c_str());
                    return -1;
                } else {
                    it->second->disconnect_all_slots();
                    it->second.reset();
                    m_sigmap.erase(it);
                }

                return 0;
            }

            inline boost::shared_ptr<boost::signals2::signal<F> > find(std::string key)
            {
                typename std::map<std::string, boost::shared_ptr<boost::signals2::signal<F> > >::iterator it = m_sigmap.find(key);
                if (it == m_sigmap.end()) {
                    Log(LERROR, "can't find \"%s\"\n", key.c_str());
                }
                return it->second;
            }

            int size() { return m_sigmap.size(); }

            std::map<std::string, boost::shared_ptr<boost::signals2::signal<F> > > m_sigmap;
        };
}

#endif

